package com.lanwf.admin.sys.config;

import com.jfinal.aop.Inject;
import com.jfinal.core.Path;
import com.jfinal.kit.Ret;
import com.jfinal.upload.UploadFile;
import com.lanwf.admin.BaseController;
import com.lanwf.admin.sys.PermissionService;
import com.lanwf.common.model.SysConfig;
import com.lanwf.admin.utils.CommonUtil;
import com.lanwf.admin.utils.Cons;

/**
 * @Auther: Lanwf
 * @Date: 2021/12/24 10:55
 * @Description:
 */
@Path(value = "/admin/config", viewPath = "/admin/sys/config")
public class ConfigAdminController extends BaseController {

    @Inject
    PermissionService permissionService;

    public void index() {
        set("permission",permissionService.getPermission(getAccountId(),getInt(0)));
        set("config",SysConfig.dao.findFirst("select * from sys_config"));
        render("index.html");
    }

    public void update() {
        UploadFile uploadpath = getFile("uploadpath", Cons.CONFIG);
        UploadFile uploadpath1 = getFile("uploadpath1", Cons.CONFIG);
        UploadFile uploadpath2 = getFile("uploadpath2", Cons.CONFIG);

        SysConfig model = getModel(SysConfig.class, "config");
        if(uploadpath != null){
            model.setLogo(CommonUtil.getFilePath(uploadpath, Cons.CONFIG));
        }

        if(uploadpath1 != null){
            model.setLoginBg(CommonUtil.getFilePath(uploadpath1, Cons.CONFIG));
        }

        if(uploadpath2 != null){
            model.setIco(CommonUtil.getFilePath(uploadpath2,Cons.CONFIG));
        }
        model.update();
        CommonUtil.setCache("config",null);
        renderJson(Ret.ok().set("msg", "修改成功"));
    }

}

